package com.zb.vo;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.zb.entity.ZjCodeSet;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.BeanUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;

/**
 * @Author WEN
 * @Date 2023/7/26 0026
 **/
@Data
@ApiModel("编码设置类实体")
public class CodeSetVo {
    @NotNull(message = "编码设置ID不能为空")
    @ApiModelProperty(value = "ID必传", required = true, example = "")
    private Long id;

    @ApiModelProperty(value = "编码规则类型：1-数字+字母组合", required = true, example = "1")
    private Integer ruleType;

    @NotBlank(message = "编码规格前缀不能为空")
    @Length(max = 2, message = "编码规格前缀字符长度限制2个字符以内")
    @Pattern(regexp = "^[A-Za-z1-9]{1,2}$", message = "编码规格前缀不支持输入中文")
    @ApiModelProperty(value = "编码规格前缀", required = true, example = "FM")
    private String codePrefix;

    @NotBlank(message = "编码规则不能为空，且至少要选择两项规则！")
    @ApiModelProperty(value = "编码规则：1-六位产品生产日期，2-八位防伪码，3-六位随机防窜码", required = true, example = "1,2")
    private String codeRule;

    @NotBlank(message = "临时编码前缀不能为空")
    @Length(max = 2, message = "临时编码前缀字符长度限制2个字符以内")
    @Pattern(regexp = "^[A-Za-z1-9]{1,2}$", message = "临时编码前缀不支持输入中文")
    @ApiModelProperty(value = "临时编码前缀", required = true, example = "LS")
    private String temporaryPrefix;

    @NotBlank(message = "临时编码规则不能为空，且至少要选择两项规则！")
    @ApiModelProperty(value = "临时编码规则：1-六位产品生产日期，2-八位防伪码，3-八位随机防窜码", required = true, example = "1,2")
    private String temporRule;

    @ApiModelProperty(value = "是否启用仓储编码：false-不启用，true-启用", required = true, example = "true")
    private Boolean enableStorehouseCode;

    @NotBlank(message = "仓储编码前缀不能为空")
    @Length(max = 2, message = "编码规格前缀字符长度限制2个字符以内")
    @Pattern(regexp = "^[A-Za-z1-9]{1,2}$", message = "仓储编码前缀不支持输入中文")
    @ApiModelProperty(value = "仓储编码前缀", required = true, example = "ST")
    private String storehousePrefix;

    @NotBlank(message = "仓储编码规则不能为空")
    @ApiModelProperty(value = "仓储编码规则：1-六位产品生产日期，2-八位防伪码，3-八位随机防窜码", required = true, example = "1,2")
    private String storehouseRule;

    @ApiModelProperty(value = "是否启用编码导入：false-不启用，true-启用", required = true, example = "true")
    private Boolean enableImport;

    @NotBlank(message = "导入编码不能为空")
    @Length(max = 2, message = "导入编码字符长度限制2个字符以内")
    @Pattern(regexp = "^[A-Za-z1-9]{1,2}$", message = "导入编码不支持输入中文")
    @ApiModelProperty(value = "导入编码", required = true, example = "IM")
    private String importPrefix;

    @NotBlank(message = "批量编码不能为空")
    @Length(max = 2, message = "批量编码字符长度限制2个字符以内")
    @Pattern(regexp = "^[A-Za-z1-9]{1,2}$", message = "批量编码不支持输入中文")
    @ApiModelProperty(value = "批量编码", required = true, example = "BT")
    private String batchPrefix;

    @ApiModelProperty(value = "创建时间", hidden = true)
    private Date createTime;

    @ApiModelProperty(value = "更新时间", hidden = true)
    private Date updateTime;

    public static CodeSetVo beanToVo(ZjCodeSet codeSet) {
        CodeSetVo vo = new CodeSetVo();
        BeanUtils.copyProperties(codeSet, vo);
        vo.setCreateTime(codeSet.getGmtCreate());
        vo.setUpdateTime(codeSet.getGmtUpdate());
        return vo;
    }

    public static ZjCodeSet voToBean(CodeSetVo vo) {
        ZjCodeSet codeSet = new ZjCodeSet();
        BeanUtils.copyProperties(vo, codeSet);
        return codeSet;
    }

    public static boolean checkRule(String rule) {
        String[] rules = {"1", "2", "3"};
        if (StringUtils.isNotBlank(rule)) {
            for (String s : rule.split(",")) {
                if (!Arrays.asList(rules).contains(s)) {
                    return false;
                }
            }
        }
        return true;
    }
}
